package com.ccb.jyzx.expand.tiantouonline.service.impl;

import com.ccb.jyzx.expand.tiantouonline.bean.FileInfo;
import com.ccb.jyzx.expand.tiantouonline.dao.FileHistoryDao;
import com.ccb.jyzx.expand.tiantouonline.service.FileHistoryService;
import com.ccb.jyzx.expand.tiantouonline.service.FileInfoService;
import com.ccb.jyzx.expand.tiantouonline.view.request.FileHistoryInfoPageRequestBody;
import com.ccb.jyzx.expand.tiantouonline.view.response.FileHistoryInfo;
import com.ccb.jyzx.expand.tiantouonline.view.response.PageResponseReult;
import com.ccb.jyzx.expand.tiantouonline.view.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * @author XIEFEI
 * @date 2019-11-22 16:35
 * @Description
 **/
@Service
public class FileHistoryServiceImpl implements FileHistoryService {

    @Autowired
    FileHistoryDao fileHistoryDao;

    @Autowired
    FileInfoService fileInfoService;

    @Value("${file_cache_dir}")
    private String file_cache_dir;

    @Value("${nas_home_path}")
    private String nas_home_path;

    @Override
    public PageResponseReult<FileHistoryInfo> findByPageParam(FileHistoryInfoPageRequestBody json) {

        PageResult<FileHistoryInfo> page = new PageResult<>(json.getPageNo(), json.getPageSize());

        int count = fileHistoryDao.countByParam(json);
        page.setTotalCount(count);
        json.setStartIndex(page.getStartIndex());
        List<FileHistoryInfo> data= fileHistoryDao.getByPageParam(json);
        if (data!=null&&data.size()>0){
            data.forEach(f->{
                String fileId = f.getFileId();
                FileInfo byId = fileInfoService.getById(fileId);
                //先查缓存目录
                File pdf = new File(file_cache_dir,byId.getSavePath()+".pdf");
//              System.out.println(pdf.getPath());
                if (pdf.exists()){//先查缓存文件夹
                    f.setPdfTag("1");
                }else{
                    pdf = new File(file_cache_dir,byId.getSavePath()+".pdf");
                    if (pdf.exists()){//在查nas文件夹
                        f.setPdfTag("1");
                    }
                }
            });
        }
        page.setData(data);
        return new PageResponseReult<FileHistoryInfo>(page);
    }
}
